package com.example.learningnote;

import com.example.learningnote.BitmapFactory;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
import java.util.Locale;

import cn.hutool.core.date.ChineseDate;

public class MiWidgetActivity extends AppCompatActivity {
    private TextView dayText;
    private TextView weekDayText;
    private TextView fullDateText;
    private TextView monthText;
    private TextView lunarDateText;
    private LinearLayout outerLayout; // 最外层 LinearLayout
    private LinearLayout bottomInfoContainer; // bottom_info_container

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_mi_widget);

        // 初始化视图
        initViews();
        // 更新日期显示
        updateDateDisplay();

        // 设置高斯模糊层高度
        adjustBottomInfoHeight();
    }

    private void initViews() {
        dayText = findViewById(R.id.day_text); // 需要在布局文件中添加对应的id
        weekDayText = findViewById(R.id.weekday_text);
        fullDateText = findViewById(R.id.full_date_text);
        monthText = findViewById(R.id.month_text);
        lunarDateText = findViewById(R.id.lunar_date_text);

        // 获取布局引用
        outerLayout = findViewById(R.id.outLayout);
        bottomInfoContainer = findViewById(R.id.bottom_info_container);
    }

    private void adjustBottomInfoHeight() {
        //  使用 ViewTreeObserver 确保布局测量完成
        outerLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                // 获取最外层布局的高度
                int outerHeight = outerLayout.getHeight();
                if (outerHeight > 0) { // 确保高度有效
                    // 计算 35% 的高度
                    int targetHeight = (int) (outerHeight * 0.35);

                    // 更新 bottom_info_container 的 LayoutParams
                    ViewGroup.LayoutParams params = bottomInfoContainer.getLayoutParams();
                    params.height = targetHeight;
                    bottomInfoContainer.setLayoutParams(params);

                    // 暂时使用静态图测试
                    Bitmap srcBitmap = android.graphics.BitmapFactory.decodeResource(getResources(), R.drawable.test);
                    setBlurredBackground(srcBitmap);

                    // 移除监听器，避免重复调用
                    outerLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            }
        });
    }

    private void setBlurredBackground(Bitmap srcBitmap){
        // 获取原始 Bitmap 的宽度和高度
        int width = srcBitmap.getWidth();
        int height = srcBitmap.getHeight();
        int cropHeight = (int) (height * 0.35);
        int startY = height - cropHeight;
        Bitmap croppedBitmap = Bitmap.createBitmap(srcBitmap, 0, startY, width, cropHeight);
        // 调用自定义的 BitmapFactory.fastBlur 方法生成模糊图
        Bitmap blurredBitmap = com.example.learningnote.BitmapFactory.fastBlur(getApplicationContext(), croppedBitmap, 80);
        BitmapDrawable blurredDrawable = new BitmapDrawable(getResources(), blurredBitmap);
        bottomInfoContainer.setBackground(blurredDrawable);
    }

    private void updateDateDisplay() {
        Calendar calendar = Calendar.getInstance();

        // 获取日期
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String dayStr = day < 10 ? "0" + day : String.valueOf(day);
        dayText.setText(dayStr);

        // 获取星期
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        weekDayText.setText(weekDays[weekDay]);

        // 获取完整日期
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        String fullDate = String.format(Locale.getDefault(), "%d.%02d.%02d", year, month, day);
        fullDateText.setText(fullDate);

        // 获取月份缩写
        String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        monthText.setText("/" + months[calendar.get(Calendar.MONTH)]);

        // 获取农历日期
        // Lunar lunar = new Lunar(calendar);
        // lunarDateText.setText(lunar.toString());

        ChineseDate chineseDate = new ChineseDate(calendar.getTime());
        String lunarStr = "农历" + chineseDate.getChineseMonthName() +
                chineseDate.getChineseDay();
        lunarDateText.setText(lunarStr);


    }

    // 农历日期转换工具类 精确度差  暂时使用第三方库
    private static class Lunar {
        private int year;
        private int month;
        private int day;
        private boolean leap;
        // 农历月份
        private static final String[] CHINESE_NUMBER = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};
        // 农历日期
        private static final String[] CHINESE_DAY = {"初一", "初二", "初三", "初四", "初五","初六", "初七", "初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五","十六", "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"};

        public Lunar(Calendar calendar) {
            this.year = calendar.get(Calendar.YEAR);
            this.month = calendar.get(Calendar.MONTH) + 1;
            this.day = calendar.get(Calendar.DAY_OF_MONTH);
            this.leap = false;
        }

        @Override
        public String toString() {
            return "农历" + CHINESE_NUMBER[month - 1] + "月" + CHINESE_DAY[day - 1];
        }
    }
}
